package me.yricky.alia.ui.layoutPage

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Check
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.rememberDialogState
import me.yricky.alia.data.hierarchy.Layer
import me.yricky.alia.ui.component.ARGBColorPicker
import me.yricky.alia.ui.layoutPage.renderModel.LayerRender

/**
 * @author Yricky
 * @date 2022/2/9
 */
sealed class DialogInst{
    @Composable
    abstract fun ComposeDialog()
}
class LayerStyleDialogInst(
    private val l:Layer.LDPair,
    private val initialStyle: LayerRender.LayerStyle?,
    private val onApply:(Layer.LDPair, LayerRender.LayerStyle)->Unit,
    private val onClose:()->Unit
):DialogInst(){

    @Composable
    override fun ComposeDialog() {
        Dialog(
            onClose,
            state = rememberDialogState(width = Dp.Unspecified, height = Dp.Unspecified),
            resizable = false,
            title = l.toString()
        ){
            var color by remember{ mutableStateOf(initialStyle?.color ?: Color.Black) }
            var brush by remember{ mutableStateOf(initialStyle?.brush ?: solidBrush) }
            var stroke by remember { mutableStateOf(initialStyle?.stroke ?: false ) }
            var strokeWidth by remember { mutableStateOf(initialStyle?.strokeWidth ?: 2f) }
            var aa by remember{ mutableStateOf(initialStyle?.aa ?: false) }
            Column(Modifier
                .padding(8.dp)
                .wrapContentHeight()
                .width(480.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Row {
                    Column(Modifier.weight(1f)) {
                        ARGBColorPicker(initColor = color, onColorChange = { color = it })
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Checkbox(stroke,{stroke = it})
                            Text("Show Stroke")
                        }
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Text("W")
                            Slider(
                                strokeWidth,
                                {strokeWidth = it},
                                enabled = stroke,
                                valueRange = 1f..3f,
                                colors = SliderDefaults.colors(
                                    activeTrackColor = Color.Black
                                )
                            )
                        }
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Checkbox(aa,{aa = it})
                            Text("Anti alias")
                        }
                    }
                    Spacer(Modifier.weight(0.1f))
                    val brushes by remember { mutableStateOf(internalBrushes.plus(Pair("solid", solidBrush)).toList()) }
                    LazyColumn(Modifier.weight(1f)) {
                        items(brushes){
                            Row(Modifier
                                .fillMaxWidth()
                                .clickable { brush = it.second },
                                verticalAlignment = Alignment.CenterVertically
                            ){
                                LayerIcon(
                                    Modifier.size(36.dp).padding(4.dp),
                                    it.second, color,stroke, strokeWidth,aa
                                )
                                Text(it.first)
                                if(brush == it.second){
                                    Icon(Icons.Rounded.Check,"")
                                }
                            }
                        }
                    }
                }
                Row(Modifier.fillMaxWidth()) {
                    Button({onClose()}){ Text("Cancel") }
                    Spacer(Modifier.weight(1f))
                    Button({onApply(l, LayerRender.LayerStyle(
                        color,
                        brush,
                        stroke,
                        strokeWidth,
                        aa
                    )
                    )}){
                        Text("Apply")
                    }
                    Spacer(Modifier.size(8.dp))
                    Button({
                        onApply(l, LayerRender.LayerStyle(
                            color,
                            brush,
                            stroke,
                            strokeWidth,
                            aa
                        ))
                        onClose()
                    }){
                        Text("OK")
                    }
                }

            }
        }
    }

}